package com.fishing.controller;

import QL.*;
import com.alibaba.fastjson.JSONObject;
import com.fishing.entity.EditLog;
import com.fishing.service.EditLogService;
import com.fishing.service.RecordService;
import com.fishing.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class RoomConfigController {

    private static QLClient client = new QLClient();

    @Autowired
    RecordService recordService;

    @Autowired
    EditLogService editLogService;

    //region 修改血池
    @RequestMapping("room/editHP")
    public ModelAndView editHPView(){
        return new ModelAndView("room/editHP");
    }
    //endregion

    //region 房间列表
    @ResponseBody
    @RequestMapping(value="room/getRoom",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public JSONObject getRoom(HttpServletRequest request,
                              String gameIds
    ) {
        try {
            Map<String,Object> map = new HashMap<>();
            Integer zoneId = (Integer) request.getSession().getAttribute("ZoneId");
            //请求实体
            ButlerGetRoomDataRequest req = new ButlerGetRoomDataRequest();
            //接受返回结果
            ButlerGetRoomDataResponse rsp = new ButlerGetRoomDataResponse();
            //房间集合，储存结果
            List<ButlerRoomData> data = new ArrayList<>();
            //字符串切割，分出gameId
            String[] split = gameIds.trim().split(",");
//            req.GameID = 8;
//            req.ZoneId = zoneId;
//            if (req.ZoneId < 0)
//                QLClient.ServerUrl = "http://118.24.113.66:8000/router/rest";
//            QLClient.Execute(req, rsp);
            for (String s : split) {
                Long gameId = Long.valueOf(s.trim());
                req.GameID = gameId;
                req.ZoneId = zoneId;
                if (req.ZoneId < 0)
                    QLClient.ServerUrl = "http://118.24.113.66:8000/router/rest";
                QLClient.Execute(req, rsp);

                String gameName = StringUtil.gameName(gameId);
                if (rsp.Data != null && rsp.Data.size() != 0){
                    for (ButlerRoomData datum : rsp.Data) {
                        String level = StringUtil.level(datum.RoomID);
                        datum.setGameId(gameId);
                        datum.setGameName(gameName+level);
                    }
                    data.addAll(rsp.Data);
                }
            }
            map.put("data", data);
            map.put("count",data.size());
            map.put("status", "200");
            map.put("message", "");
            return new JSONObject(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //endregion

    //region 房间血池修改
    @ResponseBody
    @RequestMapping(value="room/updateHP",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String editHP(HttpServletRequest request, Long gameId,Long roomNum,Long hp) {
            ButlerUpdateRoomDataRequest req = new ButlerUpdateRoomDataRequest();
            req.GameID = gameId;
            req.ZoneId = (Integer) request.getSession().getAttribute("ZoneId");
            req.RoomNum = roomNum;
            req.HP = hp;
            System.out.println(req.HP);
            ButlerUpdateRoomDataResponse rsp = new ButlerUpdateRoomDataResponse();
            if (req.ZoneId < 0)
                QLClient.ServerUrl = "http://118.24.113.66:8000/router/rest";
            QLClient.Execute(req, rsp);
            System.out.println(rsp.Result);
            boolean flag = false;
            String result = null;
            if (rsp.Result == 0) {
                flag = true;
                result = "success";
            }else if (rsp.Result == 1){
                result = "fail";
            }
            logRecord(request,flag,"修改血池数量为："+hp,roomNum,StringUtil.gameName(gameId));
        return result;
    }
    //endregion



    //region 日志记录
    private void logRecord(HttpServletRequest request,boolean flag,String msg,Long roomNum,String gameName){
        EditLog editLog = new EditLog();
        editLog.setAdminId((String) request.getSession().getAttribute("Admintruename"));
        editLog.setGameName(gameName);
        editLog.setRoomId(roomNum.intValue());
        editLog.setEvent(msg);
        editLog.setResult(flag ? "成功" : "失败");
        editLog.setTime(new Date());
        editLog.setIp((String) request.getSession().getAttribute("IP"));
        editLogService.insertSelective(editLog);
    }
    //endregion

    //region 查询玩家资源修改日志
    @ResponseBody
    @RequestMapping("room/queryRoomEditLog")
    public Map<String,Object> queryRoomEditLog(String adminId,Integer pageIndex,Integer pageCount){
        Map<String,Object> map = new HashMap<>();
        pageIndex = (pageIndex - 1) * pageCount;
        List<EditLog> editLogs = editLogService.selectAll(adminId,pageIndex,pageCount);
        Integer count = editLogService.selectCount();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (EditLog editLog : editLogs) {
            editLog.setTimeFormat(simpleDateFormat.format(editLog.getTime()));
        }
        map.put("data", editLogs);
        map.put("count",count);
        map.put("status", "200");
        map.put("message", "");
        return map;
    }
    //endregion


}
